package com.global.college.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.global.college.utils.GooglePlayUtil;
import com.google.gson.Gson;
import com.global.college.CollegeApplication;
import com.global.college.R;
import com.global.college.UserData;
import com.global.college.api.AliPayURLs;
import com.global.college.base.BaseActivity;
import com.global.college.base.PayResult;
import com.global.college.base.baselist.EventBusBean;
import com.global.college.constants.Constant;
import com.global.college.ui.bean.GoodsDetailModel;
import com.global.college.ui.bean.OrderCreateModel;
import com.global.college.ui.bean.WxPayModel;
import com.global.college.ui.bean.requestBean.AlipayParams;
import com.global.college.ui.bean.requestBean.WxParams;
import com.global.college.ui.interfaces.IPayOrderView;
import com.global.college.ui.interfaces.IPayView;
import com.global.college.ui.presenter.PayOrderPresenter;
import com.global.college.ui.presenter.PayPresenter;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.widget.FrescoImageView;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pay.AuthResult;

/**
 * 提交订单
 */
public class OrderCommitActivity extends BaseActivity implements IPayView, IPayOrderView {


    @BindView(R.id.fiv_icon)
    FrescoImageView fivIcon;
    @BindView(R.id.tv_goods_title)
    TextView tvGoodsTitle;
    @BindView(R.id.tv_goods_price)
    TextView tvGoodsPrice;
    @BindView(R.id.rl_goods_detail)
    RelativeLayout rlGoodsDetail;
    @BindView(R.id.tv_goods_type)
    TextView tvGoodsType;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.tv_goods_num)
    TextView tvGoodsNum;
    @BindView(R.id.tv_goods_discount)
    TextView tvGoodsDiscount;
    @BindView(R.id.tv_bottom_price)
    TextView tvBottomPrice;
    @BindView(R.id.ll_bottom_price)
    LinearLayout llBottomPrice;
    @BindView(R.id.tv_bottom_pay)
    TextView tvBottomPay;
    @BindView(R.id.ll_bottom_pay)
    LinearLayout llBottomPay;

    private PayPresenter payPresenter;
    private PayOrderPresenter payOrderPresenter;
    private String goodId;
    private String trainId;
    private OrderCreateModel orderCreateModel;
    private GoodsDetailModel mGoodsDetailModel;

    @Override
    public int getContentViewId() {
        return R.layout.activity_order_commit;
    }



    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(false);
        EventBus.getDefault().register(this);
        goodId = getIntent().getStringExtra(Constant.Key.NAME);
        trainId = getIntent().getStringExtra("trainId");
        payPresenter = new PayPresenter(this);
        payPresenter.setPayView(this);
        payOrderPresenter = new PayOrderPresenter(this);
        payOrderPresenter.setPayView(this);
        payPresenter.getGoodsDetail(goodId);
    }

    @Override
    public void getInitData() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        useImmersionBar = false;
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.iv_back,R.id.rl_goods_detail,R.id.ll_bottom_price, R.id.ll_bottom_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_goods_detail:
                break;
            case R.id.ll_bottom_price:
                break;
            case R.id.ll_bottom_pay:
                if (null != orderCreateModel) {
                    // TODO: 2024/7/18 调用google支付
                    GooglePlayUtil.getInstance().callGooglePay(orderCreateModel.getGoodId(), orderCreateModel.getOrderId());
                }
                break;
        }
    }

    private void createOrder() {
        payPresenter.createOrder(UserData.getInstance().getUserId(), goodId);
    }

    @Override
    public void onPayCallBackSuccess(Object object) {

    }

    @Override
    public void onPayCallBackFail(String error) {

    }

    @Override
    public void onGoogleResumeSuccess(String object) {
        startActivity(new Intent(OrderCommitActivity.this, PaySuccessActivity.class).putExtra("trainId", trainId));
        finish();
    }

    @Override
    public void onGooglePayFail(String error) {
        JugeAndOpenActivity(PayFailActivity.class);
    }

    @Override
    public void onGoodsDetailSuccess(GoodsDetailModel object) {
        mGoodsDetailModel = object;
        createOrder();

    }

    private void upodateUI(OrderCreateModel orderCreateModel) {
        fivIcon.setImageURI(orderCreateModel.getGoodCover());
        tvGoodsTitle.setText(orderCreateModel.getGoodName());
        tvGoodsPrice.setText("¥" + StringUtils.getPrice2(String.valueOf(orderCreateModel.getGoodPrice())));
        //商品类型，1课程2训练营
//        switch (orderCreateModel.getGoodType()) {
//            case "1":
//                tvGoodsType.setText("类型：课程");
//                break;
//            case "2":
//                tvGoodsType.setText("类型：训练营");
//                break;
//        }
        tvGoodsType.setText("训练营");
        tvTotalPrice.setText("¥" + StringUtils.getPrice2(String.valueOf(orderCreateModel.getGoodPrice())));
        tvGoodsNum.setText("1");
        tvGoodsDiscount.setText("¥" + StringUtils.getPrice2(String.valueOf(orderCreateModel.getGoodPreferentialPrice())));
        tvBottomPrice.setText("¥" + StringUtils.getPrice2(String.valueOf(orderCreateModel.getOrderPrice())));


    }

    @Override
    public void onGoodsDetailFail(String error) {

    }

    @Override
    public void onCreateOrderSuccess(OrderCreateModel orderCreateModel) {
        this.orderCreateModel = orderCreateModel;
        upodateUI(orderCreateModel);
    }

    @Override
    public void onCreateOrderFail(String error) {
        if (error.equals("当前用户有未支付订单。")){
            JugeAndOpenActivity(OrderCenterActivity.class);
            finish();
            return;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCallBack(EventBusBean event) {
        if (event != null) {
            switch (event.getType()) {
                case Constant.EventBus.APY_SUCCESS:
                    //支付成功，消耗商品
                    payOrderPresenter.googlePayResume(new AlipayParams());
                    break;
                case Constant.EventBus.APY_FAIL:
                    JugeAndOpenActivity(PayFailActivity.class);
                    break;
                default:
                    break;
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}